package com.bobo.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.CallSuper;
import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.bobo.base.widget.CustomTitleLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.trello.rxlifecycle3.LifecycleProvider;
import com.trello.rxlifecycle3.LifecycleTransformer;
import com.trello.rxlifecycle3.RxLifecycle;
import com.trello.rxlifecycle3.android.ActivityEvent;
import com.trello.rxlifecycle3.android.RxLifecycleAndroid;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

/**
 * 描述：
 * 作者: sheng
 * 日期: 2025/7/22 11:28
 */
public abstract class BaseActivity<V extends ViewBinding> extends AppCompatActivity implements LifecycleProvider<ActivityEvent> {

    protected V binding;

    private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initTheme();
        initStatusBarStyle();
        super.onCreate(savedInstanceState);
        this.lifecycleSubject.onNext(ActivityEvent.CREATE);
        // Activity管理
//        AppManager.getInstance().addActivity(this);

        // EventBus
        if (isRegistEventBus() && !EventBus.getDefault().isRegistered(this)) {//注册EventBus
            EventBus.getDefault().register(this);
        }

        //页面接受的参数方法
        initParam();
        setContentView();
        setTitle(initTitle());
        //页面数据初始化方法
        initViews();
        // 设置一些监听
        initListeners();
        //页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
        initObservable();
        // 获取网络等数据
        initData();
    }
    // 设置主题
    protected void initTheme() {
        setTheme(R.style.AppTheme);
    }

    protected void initStatusBarStyle() {
        setStatusBarDarkFont(true);
    }

    protected void setContentView() {
        binding = getViewBinding();
        if (binding != null)
            setContentView(binding.getRoot());
    }

    @NonNull
    @CheckResult
    public final Observable<ActivityEvent> lifecycle() {
        return this.lifecycleSubject.hide();
    }

    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull ActivityEvent event) {
        return RxLifecycle.bindUntilEvent(this.lifecycleSubject, event);
    }

    @NonNull
    @CheckResult
    public final <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroid.bindActivity(this.lifecycleSubject);
    }

    @CallSuper
    protected void onStart() {
        super.onStart();
        this.lifecycleSubject.onNext(ActivityEvent.START);
    }

    @CallSuper
    protected void onResume() {
        super.onResume();
        this.lifecycleSubject.onNext(ActivityEvent.RESUME);
    }

    @CallSuper
    protected void onPause() {
        this.lifecycleSubject.onNext(ActivityEvent.PAUSE);
        super.onPause();
    }

    @CallSuper
    protected void onStop() {
        this.lifecycleSubject.onNext(ActivityEvent.STOP);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.lifecycleSubject.onNext(ActivityEvent.DESTROY);
        if (isRegistEventBus())
            EventBus.getDefault().unregister(this);//取消注册EventBus
//        AppManager.getInstance().removeActivity(this);
    }

    /**
     * 设置标题组件
     *
     * @param titleView
     */
    private void setTitle(ViewGroup titleView) {
        if (titleView != null) {
            if (binding.getRoot() instanceof LinearLayout) {
                LinearLayout.LayoutParams titleBarParams = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                titleView.setLayoutParams(titleBarParams);
            }
            ((ViewGroup) binding.getRoot()).addView(titleView, 0);
        }
    }

    protected ViewGroup initTitle() {
        return initTitle("");
    }

    /**
     * 默认标题栏
     *
     * @param name
     * @return
     */
    protected ViewGroup initTitle(String name) {
        return initTitle(name, R.color.app_blue);
    }

    protected ViewGroup initTitleNative(String name) {
        return initTitle(name, R.drawable.title_bg_native);
    }

    protected ViewGroup initTitleShop(String name) {
        return initTitle(name, R.drawable.title_bg_shop);
    }

    @SuppressLint("ResourceType")
    protected ViewGroup initTitle(String name, int backgroundRes) {
        setStatusBarDarkFont(false);
        CustomTitleLayout titleLayout = new CustomTitleLayout.Builder(this)
                .setBackgroundRes(backgroundRes)
                .setTitleColor(R.color.app_color_white)
                .setLeftRes(R.mipmap.ic_back_white)
                .setTitle(name)
                .setLeftCallback(() -> {
                    onClickBack();
                })
                .build();
        return titleLayout;
    }

    @SuppressLint("ResourceType")
    protected ViewGroup initTitleWhite(String name) {
        setStatusBarDarkFont(false);
        CustomTitleLayout titleLayout = new CustomTitleLayout.Builder(this)
                .setTitle(name)
                .setLeftCallback(() -> {
                    onClickBack();
                })
                .build();
        return titleLayout;
    }


    /**
     * 设置状态栏
     *
     * @param isDark false默认白色，true为黑色
     */
    protected void setStatusBarDarkFont(boolean isDark) {
        ImmersionBar.with(this, false)
//                    .statusBarColorTransformEnable(true)
//                    .statusBarColor(R.color.white)
                .statusBarDarkFont(isDark)
                .init();
    }

    /**
     * 设置布局
     *
     * @return
     */
    protected abstract V getViewBinding();

    /**
     * 初页面接受的参数方法
     */
    protected void initParam() {

    }

    /**
     * 是否需要接收EventBus通知
     */
    protected boolean isRegistEventBus() {
        return false;
    }

    /**
     * 初始化控件
     */
    protected abstract void initViews();

    /**
     * 初始化监听
     */
    protected abstract void initListeners();

    /**
     * 页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
     */
    protected void initObservable() {

    }

    /**
     * 点击返回按钮
     */
    protected void onClickBack() {
        finish();
    }

    /**
     * 点击左边文字按钮
     */
    protected void onClickLeftStr() {
        finish();
    }

    /**
     * 点击右边边文字按钮
     */
    protected void onClickRight() {

    }

    /**
     * 获取网络等数据
     */
    protected abstract void initData();

}
